package org.com.app.knock.fragment;

import java.io.UnsupportedEncodingException;

import org.com.app.knock.GcmBroadcastReceiver;
import org.com.app.knock.activity.SplashActivity;
import org.com.app.knock.activity.UerMainActivity;
import org.json.JSONObject;

import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.acv.libs.nbase.BaseFragment;
import com.knock.app.R;

public class SplashFragment extends BaseFragment implements AnimationListener {
	protected static final String TAG = null;

	public SplashFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		CommonAndroid.getView(view, R.id.btn_back).setOnClickListener(this);
		GcmBroadcastReceiver.getDeviceToken(getActivity());
		test(view);
		ImageView imageView = (ImageView) view.findViewById(R.id.btn_back);
		Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.btn_back);
		Matrix matrix = new Matrix();
		matrix.postRotate(180);
		Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),
		        matrix, true);
		imageView.setImageBitmap(rotated);
	}
	
	private void test(View view) {
		LogUtils.e(TAG, "w----" + SplashActivity.width);
		LogUtils.e(TAG, "h----" + SplashActivity.height);
		BitmapDrawable bmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.bg_a);
		float bmapWidth = bmap.getBitmap().getWidth();
		float bmapHeight = bmap.getBitmap().getHeight();
		 
		float wRatio = SplashActivity.width / bmapWidth;
		float hRatio = SplashActivity.height / bmapHeight;
		 
		float ratioMultiplier = wRatio;
		// Untested conditional though I expect this might work for landscape mode
//		if (hRatio < wRatio) {
//			ratioMultiplier = hRatio;
//		}
		 
		int newBmapWidth = (int) (bmapWidth*ratioMultiplier);
		int newBmapHeight = (int) (bmapHeight*ratioMultiplier);
		 
//		ImageView iView = (ImageView) view.findViewById(R.id.iv_background);
//		iView.setLayoutParams(new LinearLayout.LayoutParams(newBmapWidth, newBmapHeight));
		
		
		String character = "1F324";
		char[] abc = Character.toChars(Integer.parseInt(character , 16));
		System.out.println(character);
		try {
		    // Convert from Unicode to UTF-8
		    String string = "1F324";
//		    byte[] utf8 = string.getBytes("UTF-16");

		    // Convert from UTF-8 to Unicode
//		    string = new String(utf8, "UTF-8");
		    String string2 = new String(string.getBytes("UTF-8"), "UTF-8");
		    System.out.println(string2);
		    
//		    byte[] bytes = ...
//		    String string = new String(bytes, "UTF-16");
		} catch (UnsupportedEncodingException e) {
		}
		
//		int c = 1F591;
//
//		String x = Character.toString(c);
		/*int codePoint = 128149;
		char[] charPair = Character.toChars(codePoint);

		String str = new String(charPair, 0, 2);
		System.out.println("First code point: " + str.codePointAt(0));  
		
			try {
				String[] encodings = { "Cp1252", // Windows-1252
				    "UTF-8", // Unicode UTF-8
				    "UTF-16BE" // Unicode UTF-16, big endian
				};

				String poundSign = "1F591";
				for (String encoding : encodings) {
				  System.out.format("%10s%3s  ", encoding, poundSign);
				  byte[] encoded = poundSign.getBytes(encoding);
				  for (byte b : encoded) {
				    System.out.format("%02x ", b);
				    LogUtils.e("data","==="+ b);
				  }
//				  System.out.println();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		
		// TODO Auto-generated method stub
//		HashMap<String, String> params = new HashMap<String, String>();
//		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_DATA, params, callback);
	}
	
	private BaseServiceCallBack callback = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.hiddenKeyBoard(getActivity());
//			CommonAndroid.showView(true, getView().findViewById(R.id.progressbar));
		};

		public void onError(String message) {
			if (!isFinish()) {
				LogUtils.e(TAG,"er==" + message);
			}
		};

		public void onSucces(String response) {
			if (!isFinish()) {
				JSONObject mainJsonObject;
				try {
					mainJsonObject = new JSONObject(response);
					String is_succes = CommonAndroid.getString(mainJsonObject,
							BaseUtils.KEY.is_success);
					LogUtils.e(TAG,mainJsonObject.toString());
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};

	@Override
	public int layoytResurce() {
		return R.layout.main;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		int idView = v.getId();
		switch (idView) {
		case R.id.btn_back:
			Intent PageDetail = new Intent(getActivity(), UerMainActivity.class);
        	getActivity().startActivity(PageDetail); 
			break;
		default:
			break;
		}
	}

	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}
	
}